﻿using Enum;
using MediatR;
using Practice.Common.Domains;
using Practice.Common.Dto;
using Practice.Common.Infrastructure.Interfaces;

namespace Practice.API.Application.Commands.Permissionw
{
    public class PermissionListHandler : IRequestHandler<PermissionListCommand, Result>
    {
        public readonly IBaseRepository<Permission> PermissionRepository;

        public PermissionListHandler(IBaseRepository<Permission> permissionRepository)
        {
            PermissionRepository = permissionRepository;
        }

        public async Task<Result> Handle(PermissionListCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.Success };
            try
            {
                var list = PermissionRepository.GetAll().Where(x => x.IsDelete == false).ToList();
                if (request.PermissionName != null)
                {
                    list = list.Where(x => x.PermissionName.Contains(request.PermissionName)).ToList();
                }

                result.Data = list;
                result.Code = (int)CommonEnumCode.Success;



            }
            catch (Exception)
            {

                result.Msg = "显示查询失败";

            }
            return result;
        }
    }
}
